#!/usr/bin/perl -W
use strict;
use DBI;

open(RSS, "<", "latest-rss.bml") or die;
my $dbh = DBI->connect("DBI:mysql:getRSS", "yura", "password");
my $sth1 = $dbh->prepare("SELECT `itemSeqNum` FROM `items` WHERE `itemId` = ?");
my $sth2 = $dbh->prepare("INSERT INTO `items`(`itemId`, `itemContent`) VALUES(?, ?)");
my $insection = 0;
my $id = "";
my $content = "";
my $added = 0;
my $skipped = 0;
while (my $line = <RSS>) {
  if ($line =~ /<item rdf:about='([^\']*)'>/) {
    $insection = 1;
    $id = $1;
  }
  if ($insection) {
    $content .= $line;
  }
  if ($line =~ /<\/item>/) {
    $sth1->execute($id);
    if ($sth1->rows < 1) {
      print "Adding Id " . $id . "\n";
      $added ++;
      $sth2->execute($id, $content);
    }
    else {
      $skipped ++;
    }
    $insection = 0;
    $content = "";
  }
}
print $added . " records added, " . $skipped . " records skipped\n";
close(RSS);
